package com.dstz.demo.rest.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.rest.BaseController;
import com.dstz.demo.core.manager.BuildMaterialsManager;
import com.dstz.demo.core.manager.VeSubClassManager;
import com.dstz.demo.core.model.BuildMaterials;
import com.dstz.demo.core.model.VeSubClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/BuildMaterials")
public class BuildMaterialsController extends BaseController<BuildMaterials> {
    @Resource
    BuildMaterialsManager buildMaterialsManager;
    @Resource
    VeSubClassManager veSubClassManager;
    @Override
    protected String getModelDesc() {
        return null;
    }
    @PostMapping("/saves")
    public ResultMsg saves(){
        BuildMaterials buildMaterials=new BuildMaterials();
        buildMaterials.setBuild_name("请选择");
        buildMaterials.setFather_id("");
        buildMaterials.setBuild_title("");
        buildMaterialsManager.saves(buildMaterials);
        return getSuccessResult("添加成功");
    }

    @GetMapping("/findlist")
    public List<BuildMaterials> findlist(){
        List<BuildMaterials> list=buildMaterialsManager.findlist();
        return list;
    }

    @GetMapping("/deletes/{id}")
    public ResultMsg deletes(@PathVariable("id")String id){
        buildMaterialsManager.deletes(id);
        List<VeSubClass> list=veSubClassManager.ById(id);
        if(list.size()>0) {
            veSubClassManager.deletess(id);
        }
        return getSuccessResult("删除成功");
    }

    @PostMapping("/updates")
    @ResponseBody
    public ResultMsg updates(@RequestBody String buildMaterials){
        String num=buildMaterials.replace("{\"buildMaterials\":","").substring(0,buildMaterials.replace("{\"buildMaterials\":","").length()-1);
        JSONArray jsonArray =  JSONObject.parseArray(num);
        List<BuildMaterials> buildMaterialsList = JSONObject.parseArray(jsonArray.toJSONString(), BuildMaterials.class);
        for(BuildMaterials buildMaterials1: buildMaterialsList){
            buildMaterialsManager.updates(buildMaterials1);
            List<VeSubClass> list=buildMaterials1.getVeSubClasses();
            for (VeSubClass ve: list) {
              veSubClassManager.updates(ve);
            }
        }
        return getSuccessResult("提交成功");
    }





}
